import { configure, makeAutoObservable } from 'mobx';
import { HouseEntity } from '@/entities/HouseEntity';

configure({
  enforceActions: 'always',
});

class HouseStore {
  constructor() {
    makeAutoObservable(this, {}, {autoBind: true});
  }

  /**
   * 当前房屋的信息
   */
  currentHouse: any | HouseEntity.IResultHouse = null;

  /**
   * 房屋总数
   */
  houseNumberTotal: number = 0;

  /**
   * 当前房东的全部房屋
   */
  landlordHouseList: any = null;

  /**
   * 房屋的收藏列表
   */
  houseCollectList: any = [];

  /**
   * 房屋市场的房屋列表
   */
  marketHouseList: any | HouseEntity.IResultHouse[] = [];

  /**
   * 房屋列表
   * @param houseList
   */
  setMarketHouseList(houseList: HouseEntity.IResultHouse[]) {
    this.marketHouseList = houseList;
  }

  /**
   * 清除房屋列表
   */
  clearMarketHouseList() {
    this.marketHouseList = [];
  }

  /**
   * 设置当前编辑的房屋信息
   * @param houses 房屋信息
   */
  setEditorCurrentHouse = (house: HouseEntity.IResultHouse) => {
    this.currentHouse = house;
  };

  /**
   * 清除当前编辑的房屋信息
   */
  clearEditorCurrentHouse = () => {
    this.currentHouse = null;
  };

  /**
   * 设置当前用户房屋总数
   * @param num 房屋总数
   */
  setHouseNumberTotal(num: number) {
    this.houseNumberTotal = num;
  }

  /**
   * 设置该房东全部房屋
   * @param housesList
   */
  setLandlordHouseList(housesList: Array<HouseEntity.IResultHouse>) {
    this.landlordHouseList = housesList;
    this.houseNumberTotal = housesList?.length;
  }

  /**
   * 清除该房东的全部房屋信息
   */
  clearLandlordHouseList() {
    this.landlordHouseList = null;
    this.houseNumberTotal = 0;
  }

  /**
   * 设置房屋收藏列表
   */
  setHouseCollectList(collectList: Array<{ houseId: number, count: number }>) {
    this.houseCollectList = collectList;
  }

  /**
   * 修改房屋收藏列表
   * @param houseId 房屋id
   * @param status 状态（0：未收藏，1：已收藏）
   */
  updateHouseCollectList(houseId: number, status: number) {
    const collect = this.houseCollectList.find(c => c.houseId === houseId);
    if (status === 0 && collect?.count != null) {
      collect.count--;
    } else if (status === 1 && collect?.count != null) {
      collect.count++;
    }
  }
}

const houseStore = new HouseStore();

export default houseStore;